#!/usr/bin/perl
# make a soft link to /usr/lib/cgi-bin/ to add it to the apache
use strict;
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;

sub create_group {
	my ($group_name) = @_;
	my $sample_create_group_request = "/data/share/code/trunk/perl/stock/stock_A/stockA_daily_task/cgi/sample_create_group_request.txt";
	open FV, "$sample_create_group_request";
	my @lines = <FV>;
	my $url = "xueqiu.com/service/poster";
	my $cookie = "";
	my $post_data = "";
	foreach my $line (@lines) {
		chomp($line);
		if ($line =~ /url=(.+)/) {
			$post_data = "url=$1";
		}
		if ($line =~ /Cookie(.+)/) {
			$cookie = "Cookie$1";
		}
	}
	$post_data =~ s/name\%5D=(.+?)\&data/name\%5D=$group_name\&data/g;
	my $cmd = "wget -nv --post-data \"$post_data\" --header \"$cookie\" \"$url\" -O /dev/null";
	#print "$cmd";
	my $res = `$cmd`;

	my $token = "";
	my $stamp= "";
	if ($post_data =~ /access_token\%5D=(.+?)\&/) {
		$token =$1;
	}
	if ($post_data =~ /data\%5B_\%5D=(\d+)/) {
		$stamp =$1;
	}

	my $qurl = "http://xueqiu.com/stock/transgroup/list.json?access_token=$token&_=$stamp";
	my $content = `curl \"$qurl\"`;
	chomp($content);
	my %hash = ();
	while ($content =~ /\"id\":(\d+),\"name\":\"(.+?)\"/g) {
		my ($id, $name) = ($1, $2);
	$hash{$name} = $id;
}
my $group_id = $hash{$group_name};
return $group_id;
}

sub buy_stock {
	my ($group_id, $code, $price, $date, $shares, $type) = @_;
	my $sample_buy_request = "/data/share/code/trunk/perl/stock/stock_A/stockA_daily_task/cgi/sample_buy_request.txt";
	open FV, "$sample_buy_request";
	my @lines = <FV>;
	my $url = "xueqiu.com/service/poster";
	my $cookie = "";
	my $post_data_addstock = "";
	my $post_data_addtrans = "";
	my $post_data_add = "";
	foreach my $line (@lines) {
		chomp($line);
		if ($line =~ /url=(.+?)addstock\.json(.+)/) {
			$post_data_addstock = "url=$1" . "addstock.json". "$2";
		}
		if ($line =~ /url=(.+?)addtrans\.json(.+)/) {
			$post_data_addtrans = "url=$1" . "addtrans.json". "$2";
		}
		if ($line =~ /url=(.+?)add\.json(.+)/) {
			$post_data_add = "url=$1" . "add.json". "$2";
		}
		if ($line =~ /Cookie(.+)/) {
			$cookie = "Cookie$1";
		}
	}

	if ($type eq "hk") {
		$post_data_addstock =~ s/pnames\%5D=(.+?)\&data/pnames\%5D=\%E6\%B8\%AF\%E8\%82\%A1\&data/g;
	} else {
		$post_data_addstock =~ s/pnames\%5D=(.+?)\&data/pnames\%5D=\%E7\%BE\%8E\%E8\%82\%A1\&data/g;
	}

	$post_data_addtrans =~ s/\%5Bdate\%5D=(.+?)\&/\%5Bdate\%5D=$date\&/g;
	$post_data_addtrans =~ s/symbol\%5D=(.+?)\&/symbol\%5D=$code\&/g;
	$post_data_addtrans =~ s/groupId\%5D=(\d+)\&/groupId\%5D=$group_id\&/g;
	$post_data_addtrans =~ s/price\%5D=(.+?)\&/price\%5D=$price\&/g;
	$post_data_addtrans =~ s/hares\%5D=(.+?)\&/hares\%5D=$shares\&/g;

	my $cmd;
	$cmd = "wget -nv --post-data \"$post_data_addstock\" --header \"$cookie\" \"$url\" -O /dev/null";
	`$cmd`;
	$cmd = "wget -nv --post-data \"$post_data_addtrans\" --header \"$cookie\" \"$url\" -O /dev/null";
	`$cmd`;
	$cmd = "wget -nv --post-data \"$post_data_add\" --header \"$cookie\" \"$url\" -O /dev/null";
	`$cmd`;
}

sub main {
	print "\r\n\r\n";
	my $group = url_param('group');
	my $code = url_param('code');
	my $price = url_param('price');
	my $date = url_param('date');
	my $shares = url_param('shares');
	my $market = url_param('market');

	my $sample_buy_request = "/data/share/code/trunk/perl/stock/stock_A/stockA_daily_task/cgi/sample_buy_request.txt";
	my $group_id = create_group($group);

	buy_stock($group_id, $code, $price, $date, $shares, $market);
	print "The End";
}

main();

